package cn.codenest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author ：Hyman
 * @date ：Created in 2021/4/20 9:19
 * @description： 普通socket的客户端
 * @modified By：
 * @version: $
 */
public class Client {

    private final static int SERVER_PORT = 10020;

    public static void main(String[] args) throws IOException {
        /*这里要填写服务端的IP地址和端口*/
        Socket socket = new Socket("127.0.0.1", SERVER_PORT);
        socket.setSoTimeout(30000);
        OutputStream os = socket.getOutputStream();
        InputStreamReader is;
        BufferedReader br;
        while (true) {
            System.out.println("ReadTest, Please Enter Data:");
            is = new InputStreamReader(System.in); //new构造InputStreamReader对象
            br = new BufferedReader(is); //拿构造的方法传到BufferedReader中，此时获取到的就是整个缓存流
            try { //该方法中有个IOExcepiton需要捕获
                String str = br.readLine();
                System.out.println("ReadTest Output:" + str);
                os.write(("" + str).getBytes());
                os.flush();
                if (str.equals("quit")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (is != null) {
            is.close();
        }
        if (br != null) {
            br.close();
        }
        socket.close();
    }
}